class Observer{
  constructor(data){
    this.walk(data)
  }
  walk(data) {
    // 1.判断data是否是对象
    if(!data || typeof data !== 'object') return
    // 2.遍历data对象的所有属性
    Object.keys(data).forEach(key => {
      this.defineReactive(data, key, data[key])
    });
  }
  defineReactive(obj, key, val){
    let self = this
    let dep = new Dep()

    this.walk(val)
    Object.defineProperty(obj, key, {
      enumerable: true,
      configurable: true,
      get() {
        // 收集依赖
        Dep.target && dep.addSbus(Dep.target)
        return val
      },
      set(newVal) {
        if(newVal === val) return

        val = newVal;
        self.walk(newVal)
        // 发送通知
        dep.notify()
      }
    })
  }
}